#!/bin/bash

function rootfs_install()
{
	echo "[$(basename $0)] ${FUNCNAME} $@"

	sudo_cmd "echo mttcan >> ${L4T_TOP}/rootfs/etc/modules"

	local TMPROOTFS=tmprootfs
	mkdir -p ${TMPROOTFS}/etc/
	mkdir -p ${TMPROOTFS}/opt/rk/
	mkdir -p ${TMPROOTFS}/opt/rk/quectel_ec20

	echo -e '#!/bin/bash\n' > ${TMPROOTFS}/etc/rc.local
	cat ./rc.local.camera \
							>> ${TMPROOTFS}/etc/rc.local
	sed "s/pwmchip3/pwmchip0/g" ./jetson_pwm.sh \
							> ${TMPROOTFS}/opt/rk/jetson_pwm.sh
	sed "s/can_port_num=2/can_port_num=4/g" ./jetson_can.sh \
							> ${TMPROOTFS}/opt/rk/jetson_can.sh
	cp ./jetson_camera.sh ./jetson_camera_all.sh \
							${TMPROOTFS}/opt/rk/
	cat ./jetson_camera.sh | grep -v "AR0233C-5200" \
							> ${TMPROOTFS}/opt/rk/jetson_camera2.sh
	sed -i "s/1920x1080/3840x2160/g"       ${TMPROOTFS}/opt/rk/jetson_camera2.sh
	sed -i "s/IMX390C-5200/AR0820C-5300/g" ${TMPROOTFS}/opt/rk/jetson_camera2.sh

	cp ./gpio.sh                  ${TMPROOTFS}/opt/rk/
	cp ./RKPE3AN1_gpio_conf.txt   ${TMPROOTFS}/opt/rk/gpio_conf.txt
	cp ./RKPE3AN1_gpio_x86_rst.sh ${TMPROOTFS}/opt/rk/gpio_x86_rst.sh

	local QUECTEL_DIR=/home/samba/samba_share/tmpshare/kangpan/A5-1-NvidiaJetson/外围硬件传感器/4G5G模块/上海移远Quectel
	sshpass -p ${SUDO_PASSWD} scp ${SSH_USER}@${SSH_SERVER}:${QUECTEL_DIR}/EC20_R2.1_QuecOpen.zip \
								 ${TMPROOTFS}/opt/rk/quectel_ec20/
	sshpass -p ${SUDO_PASSWD} scp ${SSH_USER}@${SSH_SERVER}:${QUECTEL_DIR}/EC20_EC200S_Linux_ECM拨号流程指导.pdf \
								 ${TMPROOTFS}/opt/rk/quectel_ec20/

	local TMPROOTFS_PATH=$(abs_path "${TMPROOTFS}")
	local TMPROOTFS_PATH_LEVEL=$(abs_path_level "${TMPROOTFS_PATH}")
	traverse ${TMPROOTFS_PATH} traverse_filter_none 0 traverse_exec_install ${TMPROOTFS_PATH_LEVEL} ${L4T_TOP}/rootfs
	rm -rf ${TMPROOTFS}
}

function rootfs_uninstall()
{
	echo "[$(basename $0)] ${FUNCNAME} $@"

	sudo_cmd "rm -rf ${L4T_TOP}/rootfs/etc/rc.local \
					 ${L4T_TOP}/rootfs/opt/rk"
}

#############################################################

if [[ $# -le 0 ]]; then
	exit 0
fi

case $1 in
	"install" )
		rootfs_install
		;;
	"uninstall" )
		rootfs_uninstall
		;;
esac
